//
//  MessageFrame.m
//  ChatDemo
//
//  Created by 陈石 on 15/5/13.
//  Copyright (c) 2015年 陈石. All rights reserved.
//
#define ChatMargin 10    //间隔
#define ChatIconWH 44    //头像宽高height、width
#define ChatPicWH 200    //图片宽高
#define ChatContentW 180 //内容宽度

#define ChatTimeMarginW 15 //时间文本与边框间隔宽度方向
#define ChatTimeMarginH 10 //时间文本与边框间隔高度方向

#define ChatContentTop 15    //文本内容与按钮上边缘间隔
#define ChatContentLeft 25   //文本内容与按钮左边缘间隔
#define ChatContentBottom 15 //文本内容与按钮下边缘间隔
#define ChatContentRight 15  //文本内容与按钮右边缘间隔

#define ChatTimeFont [UIFont systemFontOfSize:11]    //时间字体
#define ChatContentFont [UIFont systemFontOfSize:14] //内容字体

#import "MessageFrame.h"

@implementation MessageFrame

- (void)setMessage:(Message *)message
{

    _message = message;
    CGFloat      screenW     = [UIScreen mainScreen].bounds.size.width;
    // 1、计算时间的位置
    CGFloat      timeY       = ChatMargin;
    NSDictionary *attributes = @{NSFontAttributeName : [UIFont systemFontOfSize:11]};
    CGSize       timeSize    = [_message.strTime boundingRectWithSize:CGSizeMake(300, 100) options:NSStringDrawingUsesFontLeading attributes:attributes context:nil].size;
    CGFloat      timeX       = (screenW - timeSize.width) / 2;
    _timeF = CGRectMake(timeX, timeY, timeSize.width + ChatTimeMarginW, timeSize.height + ChatTimeMarginH);
    // 2、计算头像位置
    CGFloat iconX = ChatMargin;
    if (_message.from == MessageFromMe)
    {
        iconX = screenW - ChatMargin - ChatIconWH;
    }

    CGFloat iconY = CGRectGetMaxY(_timeF) + ChatMargin;
    _iconF = CGRectMake(iconX, iconY, ChatIconWH, ChatIconWH);

    // 3、计算ID位置
    _nameF = CGRectMake(iconX, iconY + ChatIconWH, ChatIconWH, 20);

    // 4、计算内容位置
    CGFloat contentX = CGRectGetMaxX(_iconF) + ChatMargin;
    CGFloat contentY = iconY;

    //根据种类分
    CGSize contentSize;
    switch (_message.type)
    {
        case MessageTypeText:
        {
            NSDictionary *attributes = @{NSFontAttributeName : [UIFont systemFontOfSize:14]};
            contentSize = [_message.strContent boundingRectWithSize:CGSizeMake(180, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
        }
            break;
        case MessageTypePicture:
            contentSize = CGSizeMake(150, 150);
            break;
        case MessageTypeVoice:
            contentSize = CGSizeMake(100, 20);
            break;
        case MessageTypeVideo:
            contentSize = CGSizeMake(160, 160);
        default:
            break;
    }

    if (_message.from == MessageFromMe)
    {
        contentX = iconX - contentSize.width - ChatContentLeft - ChatContentRight - ChatMargin;
    }

    _contentF = CGRectMake(contentX, contentY, contentSize.width + ChatContentLeft + ChatContentRight, contentSize.height + ChatContentTop + ChatContentBottom);

    _cellHeight = MAX(CGRectGetMaxY(_contentF), CGRectGetMaxY(_nameF)) + ChatMargin;
}

@end
